package com.ocom.mqsimple.controller;

import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.MqSimpleApiUrl;
import com.ocom.common.entity.access.EntityMjRecord;
import com.ocom.common.entity.company.EntityComDockApi;
import com.ocom.common.entity.mjrecode.MjRecord;
import com.ocom.common.enums.DockApiCodeEnum;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.enums.ssgl.SSGLMqTypeEnum;
import com.ocom.common.request.company.GetComRequestRequest;
import com.ocom.common.request.simple.ssgl.SSGLAccessRecordRequest;
import com.ocom.common.request.simple.ssgl.SSGLMqBaseRequest;
import com.ocom.common.request.simple.ssgl.SSGLNewPersonInfoRequest;
import com.ocom.common.request.srvpaccess.GetNosendRecordRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.GetComRequestResponse;
import com.ocom.common.utils.DateUtil;
import com.ocom.common.utils.DateUtils;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mqsimple.business.ssgl.impl.SSGLProductServiceImpl;
import com.ocom.mqsimple.feign.CompanyClientService;
import com.ocom.mqsimple.feign.MjrecodeClientService;
import com.ocom.redis.util.RedisUtil;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class SSGLController {

	private final Logger logger = LoggerFactory.getLogger(SSGLController.class);

	@Autowired
	private SSGLProductServiceImpl ssglProductService;
	@Autowired
	private MjrecodeClientService mjrecodeClientService;
	@Autowired
	private CompanyClientService companyClientService;
	@Resource
	private RedisUtil redisUtil;

	@PostMapping(MqSimpleApiUrl.URL_SsglMqNewPersonInfo)
	public Result newPersonInfo(@RequestBody SSGLNewPersonInfoRequest request){
		try {
			log.info("newPersonInfo=====参数：【{}】", request);
			SSGLMqBaseRequest baseRequest = new SSGLMqBaseRequest();

			baseRequest.setType(SSGLMqTypeEnum.NEW_PERSONINFO.getValue());

			String dockApiCode = DockApiCodeEnum.ssgl_newPersonInfo.getValue();
			Result comDockApiResult = companyClientService.getComDockApi("ssgl", request.getComId(),dockApiCode, 1);
			EntityComDockApi entityComDockApi = JSON.parseObject(JSON.toJSONString(comDockApiResult.getData()), EntityComDockApi.class);

			if (null != entityComDockApi && 0 < entityComDockApi.getAutoSize()) {
				request.setAutoSize(entityComDockApi.getAutoSize());
			}
			baseRequest.setMessage(JSON.toJSONString(request));
			ssglProductService.handle(JSON.toJSONString(baseRequest));
		}catch (Exception e){
			logger.error("发送mq失败  错误【{}】",e);
			return ResultUtil.error(ResultEnum.Err_4006);
		}
		return ResultUtil.success();
	}

	@PostMapping(MqSimpleApiUrl.URL_SsglMqAccessRecord)
	public Result accessRecord(@RequestBody SSGLAccessRecordRequest request){
		try {
			log.info("accessRecord=====参数：【{}】", request);
			String dockApiCode = DockApiCodeEnum.ssgl_accessRecord.getValue();

			String mqKey = "access_send/ssgl" + request.getComId();
			Object getRedisObj = redisUtil.get(mqKey);
			if (null == getRedisObj) {
				redisUtil.set(mqKey,request,Long.valueOf(60*60*24));
			}
			Result comDockApiResult = companyClientService.getComDockApi("ssgl", request.getComId(),dockApiCode, 1);
			EntityComDockApi entityComDockApi = JSON.parseObject(JSON.toJSONString(comDockApiResult.getData()), EntityComDockApi.class);
			int size = 500;
			if (null != entityComDockApi && 0 < entityComDockApi.getAutoSize()) {
				size = entityComDockApi.getAutoSize();
			}
			GetNosendRecordRequest getNosendRecordRequest = new GetNosendRecordRequest();
			getNosendRecordRequest.setComId(request.getComId());
			getNosendRecordRequest.setTarget(1);
			Date date = DateUtil.getDateBefore(new Date(), 15);
			String dateTime = DateUtils.dateToStr(date);
			getNosendRecordRequest.setBeginTime(dateTime);
			getNosendRecordRequest.setSize(size);
			getNosendRecordRequest.setYearDate(Arrays.asList(DateUtils.parseDateToStr("yyyyMM", date), DateUtils.parseDateToStr("yyyyMM", new Date())));

			log.info("getNosendRecord=====参数：【{}】", getNosendRecordRequest.toString());
			Result recordResult = mjrecodeClientService.getNosendRecord(getNosendRecordRequest);
			log.info("getNosendRecordResult=====参数：【{}】", recordResult);

			GetComRequestRequest getComRequestRequest = new GetComRequestRequest();
			getComRequestRequest.setComId(request.getComId());
			getComRequestRequest.setDockCode("ssgl");
			getComRequestRequest.setDockApiCode(dockApiCode);
			log.info("accessRecord==getComRequest=====参数：【{}】", getComRequestRequest);
			Result comResult = companyClientService.getComRequest(getComRequestRequest);

			if (200 == recordResult.getCode() && 200 == comResult.getCode()) {
				List<MjRecord> getNosendRecordList = JSON.parseArray(JSON.toJSONString(recordResult.getData()), MjRecord.class);
				GetComRequestResponse response = JSON.parseObject(JSON.toJSONString(comResult.getData()), GetComRequestResponse.class);

				for (MjRecord mjRecord : getNosendRecordList) {
					SSGLMqBaseRequest baseRequest = new SSGLMqBaseRequest();
					baseRequest.setType(SSGLMqTypeEnum.ACCESS_RECORD.getValue());
					request.setPId(mjRecord.getPId());
					request.setAreaId(mjRecord.getAreaId());
					request.setDeviceNo(mjRecord.getDeviceNo());
					request.setDeviceName(mjRecord.getDeviceName());
					request.setAccessTime(mjRecord.getOpenTime());
					request.setOpenType(mjRecord.getOpenType());
					request.setRequestUrl(response.getRequestUrl());
					request.setMonthValue(mjRecord.getMonthValue());
					request.setId(mjRecord.getId());
//					baseRequest.setMessage(JSON.toJSONString(request));

					String accessRecordKey = "MqSimple:accessRecord:"+ System.currentTimeMillis()+ ":" + request.getId();
					redisUtil.set(accessRecordKey,JSON.toJSONString(request),3600L);

					baseRequest.setMessage(accessRecordKey);

					log.info("ssglProductService.handle=====参数：【{}】", baseRequest);
					ssglProductService.handle(JSON.toJSONString(baseRequest));
				}
			}

		}catch (Exception e){
			logger.error("发送mq失败  错误【{}】",e);
			return ResultUtil.error(ResultEnum.Err_4006);
		}
		return ResultUtil.success();
	}




}
